﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;
using Function;
using System.Data;
using System.Text;

namespace UI.Module.OutOfSet
{
    public partial class OutOfSetList : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string a_shopid = Request.QueryString["shopID"] ?? "";

                int rowNum = 0;
                //投资者下的门店列表信息
                string shopID = "";
                StringBuilder sb = new StringBuilder();
                DataSet shoplist = new GX_InvestorShopBLL().GetList("InvestorID=" + Method.CurrentUserID);
                foreach (DataRow item in shoplist.Tables[0].Rows)
                {
                    shopID += item["ShopID"] + ",";
                }
                if (shopID != "")
                {

                    DataSet shopds = new ST_ShopBLL().GetList("ID in(" + ExtensionMethod.subLastChar(shopID) + ")");
                    foreach (DataRow item1 in shopds.Tables[0].Rows)
                    {
                        if (a_shopid == "")
                        {
                            if (rowNum == 0)
                            {
                                HiddenField1.Value = item1["ID"].ToString();
                                sb.Append("<li><a class='tab1' href='OutOfSetList.aspx?shopID=" + item1["ID"] + "'>" + item1["STitle"] + "</a></li>");
                            }
                            else
                            {
                                sb.Append("<li><a class='tab2' href='OutOfSetList.aspx?shopID=" + item1["ID"] + "'>" + item1["STitle"] + "</a></li>");
                            }
                            rowNum++;
                        }
                        else
                        {
                            if (a_shopid == item1["ID"].ToString())
                            {
                                HiddenField1.Value = a_shopid;
                                sb.Append("<li><a class='tab1' href='OutOfSetList.aspx?shopID=" + item1["ID"] + "'>" + item1["STitle"] + "</a></li>");
                            }
                            else
                            {
                                sb.Append("<li><a class='tab2' href='OutOfSetList.aspx?shopID=" + item1["ID"] + "'>" + item1["STitle"] + "</a></li>");
                            }
                        }
                    }
                    shopList.InnerHtml = sb.ToString();
                    OutOfSe(HiddenField1.Value);
                }
                else {
                    MessageBox_Page McBx = new MessageBox_Page();//实例化对话框
                    string RemarkString;
                    RemarkString = "暂无门店信息";

                    McBx.Title = "系统提示";
                    McBx.AddContent(RemarkString);
                    McBx.Type = MsgType.Error;
                    McBx.AddUrl("返回", "../../Module/User/UserMsge.aspx", UrlType.Href);
                    MessageEvent.MessageBox(McBx, "../../ErrorPage/ShowMsg.aspx");
                    return;
                }
            }
        }

       
        public void OutOfSe(string shopID)
        {
            //int rowNum = 0;
            //decimal sum_value = 0;
            //DataSet ds = new TJ_SellInfoByMonthBLL().GetList("shopID="+shopID);
            //if (ds.Tables[0].Rows.Count>0)
            //{
            //    rowNum = ds.Tables[0].Rows.Count;
            //    if (rowNum%12==0)
            //    {
            //        DataSet sy_ds = new TJ_SellInfoByMonthBLL().GetListByPage("shopID=" + shopID, "ID asc", ((rowNum / 12)-1)*12+1, (rowNum / 12)*12);
            //        foreach (DataRow item in sy_ds.Tables[0].Rows)
            //        {
            //            sum_value=Convert.ToDecimal(item["sy_value"]);
            //        }
            //    }
            //}
            //return sum_value;

            ST_Shop shop = new ST_ShopBLL().GetModel(shopID.Trim().ToInt32());
            KZ_ShopRate kz_ShopRate = new KZ_ShopRateBLL().GetModel(shopID.Trim().ToInt32(),6);

            //保证收益金额
            decimal PledgeGain = 0;
            //年总销售金额
            decimal zxsje = 0;
            //年总收益
            decimal zsy = 0;

            if (shop!=null && kz_ShopRate!=null)
            {
                PledgeGain=Convert.ToDecimal(kz_ShopRate.Value)*shop.JoinMoney;
            }
            TJ_SellInfoByYear model = new TJ_SellInfoByYearBLL().GetModelbyShopID(shopID.Trim().ToInt32());
            if (model!=null)
            {
                div_sy.Attributes.CssStyle.Add("display", "block");
                zsy = model.InCome;
                zxsje = model.AllOrderMoney - model.AllBackMoney;
            }
            //年销售额所在的梯级
            KZ_ShopSalesStep kz_model1 = new KZ_ShopSalesStepBLL().GetModel(shopID.Trim().ToInt32(), zxsje);
            //最低梯级
            KZ_ShopSalesStep kz_model2 = new KZ_ShopSalesStepBLL().GetModel(shopID.Trim().ToInt32());

            if (kz_model1 != null && kz_model2 != null)
            {
                if (zsy <= PledgeGain)
                {
                    Label2.Text = zsy.ToString();
                    Label3.Text = (PledgeGain - zsy).ToString();
                    Label1.Text = PledgeGain.ToString();
                }
                if (zsy > PledgeGain)
                {
                    if (kz_model1.ID == kz_model2.ID)
                    {
                        Label2.Text = zsy.ToString();
                        Label3.Text = "0";
                        Label1.Text = zsy.ToString();
                    }
                    else
                    {
                        Label2.Text = zsy.ToString();
                        Label3.Text = (zxsje * (kz_model1.Rate - kz_model2.Rate)).ToString();
                        Label1.Text = (zsy + (zxsje * (kz_model1.Rate - kz_model2.Rate))).ToString();
                    }
                }
            }
        }
        string Message = "";
        #region 数据绑定
        private void Bind(string Where_String = "")
        {
            int RecordCount = 0;
            DataTable Page_Dt = new DataTable();
            Page_Dt = BLL.PageQuery2005_BLL.SelectPage(out RecordCount, "ShopID,StatMonth,InCome", "TJ_SellInfoByMonth", "ShopID="+Where_String, "[ID] desc", Pager.CurrentPageIndex,
            Pager.PageSize, PubConstant.DataBaseTarget.JHShop, out Message);

            if (!string.IsNullOrEmpty(Message))
            {
                MessageBox_Page McBx = new MessageBox_Page();//实例化对话框
                string RemarkString;
                RemarkString = "数据查询失败";

                McBx.Title = "系统提示";
                McBx.AddContent(RemarkString);
                McBx.Type = MsgType.Error;
                McBx.AddUrl("返回", "../../Module/User/UserMsge.aspx", UrlType.Href);
                MessageEvent.MessageBox(McBx, "../../ErrorPage/ShowMsg.aspx");
                return;
            }
            rptTest.DataSource = Page_Dt;
            rptTest.DataBind();
            this.Pager.RecordCount = RecordCount; 
        }
        private void Bind1(string Where_String = "")
        {
            int RecordCount = 0;
            DataTable Page_Dt = new DataTable();
            Page_Dt = BLL.PageQuery2005_BLL.SelectPage(out RecordCount, "[id],[ShopID],(SELECT STitle FROM st_shop WHERE ID=ShopID) AS shopName,[LogTime],[OutOfItem],[Solve],[CutMoney],[Note],[CreateTime],[OpID]", "[ST_OutOfSet]", "ShopID=" + Where_String, "[ID] desc", Pager1.CurrentPageIndex,
            Pager1.PageSize, PubConstant.DataBaseTarget.JHShop, out Message);

            if (!string.IsNullOrEmpty(Message))
            {
                MessageBox_Page McBx = new MessageBox_Page();//实例化对话框
                string RemarkString;
                RemarkString = "数据查询失败";

                McBx.Title = "系统提示";
                McBx.AddContent(RemarkString);
                McBx.Type = MsgType.Error;
                McBx.AddUrl("返回", "../../Module/User/UserMsge.aspx", UrlType.Href);
                MessageEvent.MessageBox(McBx, "../../ErrorPage/ShowMsg.aspx");
                return;
            }
            GridView1.DataSource = Page_Dt;
            GridView1.DataBind();
            this.Pager1.RecordCount = RecordCount;
        }
        private void Bind2(string Where_String = "")
        {
            int RecordCount = 0;
            DataTable Page_Dt = new DataTable();
            Page_Dt = BLL.PageQuery2005_BLL.SelectPage(out RecordCount, "[ID],[ShopID],[SaleMoney],[Rate],[CreateTime],[OpID]", "[KZ_ShopSalesStep]", "ShopID=" + Where_String, "[ID] desc", Pager2.CurrentPageIndex,
            Pager2.PageSize, PubConstant.DataBaseTarget.JHShop, out Message);

            if (!string.IsNullOrEmpty(Message))
            {
                MessageBox_Page McBx = new MessageBox_Page();//实例化对话框
                string RemarkString;
                RemarkString = "数据查询失败";

                McBx.Title = "系统提示";
                McBx.AddContent(RemarkString);
                McBx.Type = MsgType.Error;
                McBx.AddUrl("返回", "../../Module/User/UserMsge.aspx", UrlType.Href);
                MessageEvent.MessageBox(McBx, "../../ErrorPage/ShowMsg.aspx");
                return;
            }
            GridView2.DataSource = Page_Dt;
            GridView2.DataBind();
            this.Pager2.RecordCount = RecordCount;
        }
        protected void Pager_BindData(object sender, EventArgs e)
        {
            Bind((HiddenField1.Value??"").ToString());
        }
        protected void Pager1_BindData(object sender, EventArgs e)
        {
            Bind1((HiddenField1.Value ?? "").ToString());
        }
        protected void Pager2_BindData(object sender, EventArgs e)
        {
            Bind2((HiddenField1.Value ?? "").ToString());
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex != -1)
            {
                int indexID = (this.Pager.CurrentPageIndex - 1) * this.Pager.PageSize + e.Row.RowIndex + 1;
                e.Row.Cells[0].Text = indexID.ToString();
            }
        }
        protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex != -1)
            {
                int indexID = (this.Pager.CurrentPageIndex - 1) * this.Pager.PageSize + e.Row.RowIndex + 1;
                e.Row.Cells[0].Text = indexID.ToString();
            }
        }
        #endregion

        protected void Button1_Click(object sender, EventArgs e)
        {

        }
    }
}